package com.zqh.irmadscore.controller;

import com.zqh.irmadscore.model.entity.TestData;
import com.zqh.irmadscore.service.FileDealService;
import com.zqh.irmadscore.utils.resultUtils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Tag(name = "文件处理接口")
@RequestMapping("/file")
public class FileUploadController {

    // 文件存储路径（根据实际需求配置）
    private static final String UPLOAD_DIR = System.getProperty("user.dir") + "/src/main/resources/static/excel_file/";

    private final FileDealService fileDealService;

    @PostMapping("/upload")
    @Operation(summary = "文件上传")
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {

       boolean b  =   fileDealService.uploadFile(file);

       return b?  Result.ok( file.getOriginalFilename() + "上传成功"): Result.ok("上传失败");

    }


    @Operation(summary = "下载文件")
    @GetMapping("downloadFile/{fileName}")
    public Object downloadFile(@PathVariable String fileName) throws IOException {


        return fileDealService.downloadFile(fileName);

    }

    @Operation(summary = "读取excel文件")
    @GetMapping("/readExcel/{fileName}")
    public Result<List<TestData>> readExcel(@PathVariable String fileName) throws IOException {

        var data = fileDealService.readExcel(fileName);

        return Result.ok(data);
    }



    @GetMapping("/test")
    public Result<Void> test() {

        String currentPath = System.getProperty("user.dir");
        System.out.println("当前路径：" + currentPath);

        return Result.ok();
    }


}